
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsParticipantEntity;
import com.j2eefast.produce.pms.mapper.PmsParticipantMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>参建单位</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsParticipantService extends ServiceImpl<PmsParticipantMapper, PmsParticipantEntity> {

	@Resource
	@Lazy
	private SerialService pmsSerialService;
	@Resource
	protected PmsParticipantMapper pmsParticipantMapper;

	Long total=0L;  //序列默认
	Long total1=0L;  //序列默认
	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String participantCode = (String) params.get("participantCode");
		String participantName = (String) params.get("participantName");
		String projectCode = (String) params.get("projectCode");
		String region = (String) params.get("region");
		String projectType = (String) params.get("projectType");
		String projectName = (String) params.get("projectName");
		String supervisorName = (String) params.get("supervisorName");
		String supervisorIphone = (String) params.get("supervisorIphone");
		String firstName = (String) params.get("firstName");
		String firstIphone = (String) params.get("firstIphone");
		String buildName = (String) params.get("buildName");
		String buildIphone = (String) params.get("buildIphone");
        String participantCode2 = (String) params.get("participantCode2");
        String participantName2 = (String) params.get("participantName2");
		String participantStatus = (String) params.get("participantStatus");
		Page<PmsParticipantEntity> page = pmsParticipantMapper.findPage(
				new Query<PmsParticipantEntity>(params).getPage(),
				StrUtil.nullToDefault(participantCode,""),
				StrUtil.nullToDefault(participantName,""),
				StrUtil.nullToDefault(projectCode,""),
				StrUtil.nullToDefault(region,""),
				StrUtil.nullToDefault(projectType,""),
				StrUtil.nullToDefault(projectName,""),
				StrUtil.nullToDefault(supervisorName,""),
				StrUtil.nullToDefault(supervisorIphone,""),
				StrUtil.nullToDefault(firstName,""),
				StrUtil.nullToDefault(firstIphone,""),
				StrUtil.nullToDefault(buildName,""),
				StrUtil.nullToDefault(buildIphone,""),
				StrUtil.nullToDefault(participantStatus,""),
                StrUtil.nullToDefault(participantCode2,""),
                StrUtil.nullToDefault(participantName2,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

    public List<PmsParticipantEntity> findList(Map<String, Object> params) {
        String participantCode = (String) params.get("participantCode");
        String participantName = (String) params.get("participantName");
        List<PmsParticipantEntity> page = pmsParticipantMapper.findList(
                new Query<PmsParticipantEntity>(params).getPage(),
                StrUtil.nullToDefault(participantCode, ""),
                StrUtil.nullToDefault(participantName, ""),
                (String) params.get(Constant.SQL_FILTER));
        return page;
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Long[] ids) {

        return pmsParticipantMapper.deleteByIds(ids);

    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateCheck(Long id) {
        Long userId = UserUtils.getUserInfo().getId();

        return pmsParticipantMapper.updateCheck(id, userId);

    }

    public SysUserEntity getSysById(@Param("userId") Long userId) {
        return pmsParticipantMapper.getSysById(userId);
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PmsParticipantEntity taskFrom) {
//		Long userId= UserUtils.getUserInfo().getId();
        Date date = new Date();//创建一个时间对象，获取到当前的时间
        taskFrom.setCreateTime(date);
        taskFrom.setUpdateTime(date);

        String userName = UserUtils.getUserInfo().getName();
        taskFrom.setCreateBy(userName);
        taskFrom.setUpdateBy(userName);
        PmsParticipantEntity pmsParticipantEntity = pmsParticipantMapper.selectParticipant();

        if (pmsParticipantEntity != null && pmsParticipantEntity.getProjectTotal() != null) {
            total = pmsParticipantEntity.getProjectTotal();
        }
        Map<String, String> projectCode = pmsSerialService.formatTitle(total, "工程编号序列");//工程编号
        taskFrom.setProjectCode(projectCode.get("title"));
        taskFrom.setProjectTotal(Long.parseLong(projectCode.get("total")));

        if (pmsParticipantEntity != null && pmsParticipantEntity.getParticipantTotal() != null) {
            total1 = pmsParticipantEntity.getParticipantTotal();
        }
        Map<String, String> participantCode = pmsSerialService.formatTitle(total1, "参建编号");//参建编号
        taskFrom.setParticipantCode(participantCode.get("title"));
        taskFrom.setParticipantTotal(Long.parseLong(participantCode.get("total")));
        return pmsParticipantMapper.save(taskFrom);

    }


}
